package com.hz.common;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;

import androidx.appcompat.app.AppCompatActivity;

import com.hz.common.activity.DestroyActivity;
import com.hz.common.net.tls.TlsClient;
import com.hz.common.utils.LogObserverUtils;
import com.hz.common.utils.HttpUtils;
import com.hz.common.utils.ThreadUtils;
import com.hz.common.utils.UtilsActivityLifecycleImpl;
import com.hz.common.view.HwAlertDialog;
import com.hz.common.view.LoadingDialog;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.btn_log).setOnClickListener(v -> logObserver());
        findViewById(R.id.btn_dialog).setOnClickListener(v -> showDialog(getString(R.string.app_name)));
        findViewById(R.id.btn_url_tls).setOnClickListener(v -> UrlTls());
        findViewById(R.id.btn_kill_ac).setOnClickListener(v -> killActivity());
        findViewById(R.id.btn_loading).setOnClickListener(v -> loading());
        findViewById(R.id.btn_loading_true).setOnClickListener(v -> loading2());
    }

    // 订阅日志
    private void logObserver() {
        LogObserverUtils.getInstance().isTimeStamp(true).addObserver();
        // 模拟执行
        for (int i = 0; i < 10; i++) {
            LogObserverUtils.getInstance().log("whz", "item" + i);
        }
        // 取消观察
        LogObserverUtils.getInstance().deleteObservers(msg -> Log.e("whz", msg));
    }

    // 提示弹窗
    private void showDialog(String msg) {
        HwAlertDialog dialog = new HwAlertDialog(this, true, Gravity.BOTTOM);
        dialog.setMessage(msg);
        dialog.setIHwAlertDialogListener(new HwAlertDialog.IHwAlertDialog() {
            @Override
            public void cancelClick() {

            }

            @Override
            public void finishClick(String msg) {

            }
        });
        dialog.show();
    }

    // 域名校验
    private void UrlTls() {
        ThreadUtils.executeBySingle(new ThreadUtils.Task<String>() {
            @Override
            public String doInBackground() throws Throwable {
                TlsClient tlsClient = HttpUtils.getInstance().callTlsGet("https://b2c1.dccnet.com.cn/icbc/facets.uaf");
                return tlsClient.getResponse();
            }

            @Override
            public void onSuccess(String result) {
                showDialog(result);
            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onFail(Throwable t) {
                showDialog(t.getMessage());
            }
        });
    }

    // 定时销毁
    private void killActivity() {
        Log.e("whz", "killActivity");
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                List<Activity> activities = UtilsActivityLifecycleImpl.getInstance().getActivitiesByReflect();
                for (int i = 0; i < activities.size(); i++) {
                    Activity activity = activities.get(i);
                    String name = activity.getClass().getName();
                    Log.e("whz", "activity:" + name);
                    if (activity.getClass().equals(DestroyActivity.class)) {
                        Log.e("whz", "kill:" + name);
                        activity.finish();
                    }
                }
            }
        }, 3000);
        startActivity(new Intent(this, DestroyActivity.class));
    }

    private void loading() {
        LoadingDialog dialog = new LoadingDialog(this, false);
        dialog.show();
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                dialog.dismiss();
            }
        }, 4000);

    }

    private void loading2() {
        LoadingDialog dialog = new LoadingDialog(this, true);
        dialog.show();
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                dialog.dismiss();
            }
        }, 4000);
    }
}